package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.report;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.BaseSwipeBackFragment;
import com.kaiwukj.android.ufamily.di.component.DaggerDynamicComponent;
import com.kaiwukj.android.ufamily.di.module.DynamicModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicReportTypeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ShieldResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.DynamicContract;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.List;

import butterknife.BindView;

public class ReportCardFragment extends BaseSwipeBackFragment<DynamicReportPresenter> implements DynamicContract.ReportView {
    public static final String REPORT_PAGE = "dynamic_report";
    @BindView(R.id.rv_report_card_type)
    RecyclerView mRecyclerView;
    @BindView(R.id.qbtn_sure_report)
    QMUIRoundButton mQMUIRoundButton;
    @BindView(R.id.tv_report_user)
    TextView tvReportUser;
    @BindView(R.id.tv_report_content)
    TextView tvReportContent;
    private QMUITopBar mTopBar;
    private ReportTypeAdapter mReportTypeAdapter;
    // private int noteId;
    private DynamicResult dynamicResult;

   /* @Deprecated
    public static ReportCardFragment newInstance(int cardId) {
        ReportCardFragment fragment = new ReportCardFragment();
        fragment.noteId = cardId;
        return fragment;
    }*/

    public static ReportCardFragment newInstance(DynamicResult dynamicResult) {
        ReportCardFragment fragment = new ReportCardFragment();
        fragment.dynamicResult = dynamicResult;
        return fragment;
    }

    /**
     * 提供 AppComponent (提供所有的单例对象) 给实现类, 进行 Component 依赖
     *
     * @param appComponent
     */
    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerDynamicComponent.builder()
                .appComponent(appComponent)
                .dynamicModule(new DynamicModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_report_card;
    }

    @Override
    protected void initView() {

        mReportTypeAdapter = new ReportTypeAdapter();
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(mReportTypeAdapter);
        mReportTypeAdapter.setOnItemClickListener((adapter1, view, position) -> {
            mReportTypeAdapter.check(position);
        });

        mQMUIRoundButton.setOnClickListener(view -> {
          /*  for (int i = 0; i < mReportTypeList.size(); i++) {
                if (mReportTypeList.get(i).isChecked()) {
                    mReportCardDetailRequest.setReportTypeId(mReportTypeList.get(i).getId());
                }
            }*/
            if (dynamicResult != null) {
                assert mPresenter != null;
                // mPresenter.requestReportCardDetail(mReportCardDetailRequest);

                mPresenter.report(dynamicResult.getId(), mReportTypeAdapter.getCheckItem().getId());
            } else {
                showMessage("未获取到举报对象");
            }
        });

        loadDynamicResult();

    }

    /**
     * 初始化数据
     *
     * @param savedInstanceState
     */
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        assert mPresenter != null;
        mPresenter.fetchReportType();
    }

    private void loadDynamicResult() {
        if (dynamicResult == null) return;

        String user = dynamicResult.getUserName();
        String content = dynamicResult.getDynamicContent();

        SpannableString userString = new SpannableString(String.format("举报@%s", user));
        ForegroundColorSpan userColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.text_01));
        userString.setSpan(userColorSpan, 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvReportUser.setText(userString);

        SpannableString contentString = new SpannableString(String.format("@%s: %s", user, content));
        ForegroundColorSpan contentColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.text_01));
        contentString.setSpan(contentColorSpan, user.length() + 2 - 1, user.length() + content.length() + 2 + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvReportContent.setText(contentString);
    }

    @Override
    public void onSupportVisible() {
        if (getActivity() != null) {//屏蔽此条内容
            mTopBar = this.getActivity().findViewById(R.id.qtb_social_circle);
            initTopBar(mTopBar);
        }

    }

    private void initTopBar(QMUITopBar topBar) {
        topBar.setTitle(getString(R.string.social_circle_report_card));
        topBar.removeAllRightViews();
        topBar.removeAllLeftViews();
        topBar.addLeftBackImageButton().setOnClickListener(view -> {
            if (getActivity() != null) getActivity().onBackPressed();
        });
    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == DynamicReportPresenter.REPORT) {
            showMessage(getString(R.string.social_circle_card_report_success));
            startWithPop(ReportCardSuccessFragment.newInstance());
        }
    }

    @Override
    public void onFetchReportView(List<DynamicReportTypeResult> results) {
        mReportTypeAdapter.setList(results);
        if (mReportTypeAdapter.getItemCount() > 0) {
            mReportTypeAdapter.check(0);
        }
    }

    @Override
    public void onFetchShieldList(List<ShieldResult> results) {

    }
}
